package msr.util;

import java.util.Calendar;

/**
 * 日付のフォーマットを編集するクラス
 * */

public class DateParse {

	public DateParse() {

	}

	public String getNow() {
		String rtValue = null;
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		String s_month = String.valueOf(month);
		String s_day = String.valueOf(day);
		if (month > 0 && month < 10)
			s_month = "0" + s_month;
		if (day > 0 && day < 10)
			s_day = "0" + s_day;
		rtValue = String.valueOf(year) + s_month + s_day;
		return rtValue;
	}

	public String parseDate(String orgYear, String orgMonth, String orgDay) {
		String rtValue = null;
		String s_month = orgMonth;
		String s_day = orgDay;
		if (orgYear != null && orgMonth != null && orgDay != null) {
			int month = Integer.parseInt(s_month);
			int day = Integer.parseInt(s_day);
			if (month > 0 && month < 10)
				s_month = "0" + s_month;
			if (day > 0 && day < 10)
				s_day = "0" + s_day;
			rtValue = String.valueOf(orgYear) + s_month + s_day;
		}
		return rtValue;
	}

	public boolean compareDate(String date1, String date2) {
		boolean check = false;
		if (date1 != null && date2 != null) {
			if (Integer.parseInt(date1) > Integer.parseInt(date2))
				check = true;
		}
		return check;
	}

	public String invertParseDate(String orgDate) {
		String rtData = null;
		if (orgDate != null) {
			rtData = orgDate.substring(0, 4) + orgDate.substring(5, 7)
					+ orgDate.substring(8, 10);
		}
		return rtData;
	}

	public String gettime() {
		String rtValue = null;
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		rtValue = this.calHalfTime(year, month);
		return rtValue;
	}

	public String calHalfTime(int year, int month) {
		String rtValue = null;
		int tmpmonth;
		int tmpYear;
		if (month > 6) {
			tmpmonth = month - 6;
			tmpYear = year - 1;
		} else {
			tmpmonth = month + 6;
			tmpYear = year - 2;
		}
		rtValue = String.valueOf(tmpYear) + String.valueOf(tmpmonth) + "00";
		return rtValue;
	}
}
